﻿using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IEquipmentServices;
using MaiPlanServer.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.EquipmentServices
{
    public class RepairOrderServices : IRepairOrderServices
    {

        public RepairOrderServices(IBaseRepository BaseConn)
        {
            this.BaseConn = BaseConn;
        }

        public IBaseRepository BaseConn { get; }
        public async Task<bool> AddAsync(dv_repair model)
        {
            var result = await BaseConn.InsertAsync(model);
            return result;
        }
        public async Task<bool> Adds(dv_repair_line model)
        {
            bool bol = await BaseConn.InsertAsync(model);

            return bol;
        }
        public async Task<bool> UpdateAsync(dv_repair model)
        {
            var result = await BaseConn.UpdateAsync(model, "repair_id");
            return result;
        }

        public async Task<bool> DellAllAsync(string[] Id)
        {
            var result = await BaseConn.DeleteAsync(@"delete dv_repair where repair_id in @Id", new { Id });
            return result;
        }

        public Task<bool> DellAsync(string Id)
        {
            var result = BaseConn.DeleteAsync(@"delete dv_repair where repair_id=@Id", new { Id = Id });
            return result;
        }

        public async Task<int> GetCountAsync(string repair_name)
        {
            string sql = @"select count(1) from dv_repair  where  repair_name like @repair_name ";

            var count = await BaseConn.GetRecoreCountAsync(sql, new { repair_name = "%" + repair_name + "%" });

            return count;
        }

        public async Task<IEnumerable<dv_repair>> GetJsonAsync(int PageIndex, int PageSize, string repair_code, string repair_name, string machinery_code, string machinery_name, string repair_result, string status)
        {
            string sql = @"select * from dv_repair  where 1=1";

            if (!string.IsNullOrEmpty(repair_code))
            {
                sql += " and repair_code like @repair_code ";
            }
            if (!string.IsNullOrEmpty(repair_name))
            {
                sql += " and repair_name like @repair_name ";
            }
            if (!string.IsNullOrEmpty(machinery_code))
            {
                sql += " and machinery_code like @machinery_code ";
            }
            if (!string.IsNullOrEmpty(machinery_name))
            {
                sql += " and machinery_name like @machinery_name ";
            }
            if (!string.IsNullOrEmpty(repair_result))
            {
                sql += " and repair_result like @repair_result ";
            }
            if (!string.IsNullOrEmpty(status))
            {
                sql += " and status like @status ";
            }
            sql += " order by create_time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only ";
            var list = await BaseConn.QueryAsync<dv_repair>(sql, new { PageIndex, PageSize, repair_code = "%" + repair_code + "%", repair_name = "%" + repair_name + "%", machinery_code = "%" + machinery_code + "%", machinery_name = "%" + machinery_name + "%", repair_result = "%" + repair_result + "%", status = "%" + status + "%" });

            return list;
        }




        public async Task<string> GetCodeasync()
        {
            var CodeNo = DateTime.Now.ToString("yyyy-MM-dd");
            string sql = @"select top(1)repair_code from dv_repair where repair_code like @CodeNo  order by create_time desc ";
            var repair_code = await BaseConn.QueryFirstOrDefaultAsync<string>(sql, new { CodeNo = "%" + CodeNo + "-%" });

            var Prefixs = "REP";

            if (string.IsNullOrEmpty(repair_code))
            {
                CodeNo = $"{Prefixs}-{CodeNo}-001";
            }
            else
            {
                var numberStr = repair_code.Substring(repair_code.LastIndexOf('-') + 1);

                int num = 10000;
                if (int.TryParse(numberStr, out num))
                {
                    num += 1;
                    if (num < 10000)
                    {
                        CodeNo = $"{Prefixs}-{CodeNo}-00{num}";
                    }
                    else if (num < 10100)
                    {
                        CodeNo = $"{Prefixs}-{CodeNo}-0{num}";
                    }
                    else
                    {
                        CodeNo = $"{Prefixs}-{CodeNo}-{num}";
                    }
                }
            }

            return CodeNo;
        }

        public async Task<IEnumerable<dv_repair_line>> GetLine(string repair_id)
        {
            string sql = @"select * from dv_repair_line dl inner join dv_repair dr  on dl.repair_id = dr.repair_id where  dl.repair_id like @repair_id ";

            var count = await BaseConn.QueryAsync<dv_repair_line>(sql, new { repair_id = "%" + repair_id + "%" });

            return count;
        }







    }
}
